//
//  MaintenceNavbarView.swift
//  koolcar
//
//  Created by xiantian on 2023/10/24.
//

import UIKit

enum MaintenceItem {
    case service
    case store
    case shoppingCar
    case favory
}


typealias MaintenceNavbarViewBlock = (MaintenceItem) -> Void

class MaintenceNavbarView: UIView {
    var callBack: MaintenceNavbarViewBlock?
    public var carNumber: NSInteger = 0 {
        didSet {
            if carNumber > 0 {
                self.redView.isHidden = false
                self.redView.setup_titleLabel(text: "\(carNumber)")
                self.redView.titleLabel?.frame.size.width = 100
                self.redView.titleLabel?.sizeToFit()
                let tempW = self.redView.titleLabel?.frame.size.width ?? 0
                self.redView.snp.updateConstraints { make in
                    make.width.equalTo(tempW + 8)
                }
            }else{
                self.redView.isHidden = true
            }
            self.updateConstraints()
        }
    }
    
    private var serviceBtn: MainCustomBtn!
    private var storeBtn: MainCustomBtn!
    private var shoppingCarBtn: UIButton!
    private var likeBtn: UIButton!
    private var redView: UIButton = {
        let tempView = UIButton()
        tempView.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 9)
        tempView.setup_titleColor(color: .white)
        tempView.backgroundColor = UIColor.init(hex: 0xE8181A)
        tempView.layer.cornerRadius = 6
        tempView.layer.masksToBounds = true
        tempView.isUserInteractionEnabled = false
        return tempView
    }()
    private var bottomLineView: UIView = {
        let tempView = UIView()
        tempView.backgroundColor = Theme.lineColor
        return tempView
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        serviceBtn = MainCustomBtn()
        serviceBtn.title = MMLocalizedString("mainten_server", comment: "服务")
        serviceBtn.isSelected = true
        serviceBtn.addTarget(self, action: #selector(self.serviceBtnAction), for: .touchUpInside)
        self.addSubview(serviceBtn)
        
        storeBtn = MainCustomBtn()
        storeBtn.title = MMLocalizedString("mainten_eShop", comment: "商城")
        storeBtn.isSelected = false
        storeBtn.addTarget(self, action: #selector(self.storeBtnAction), for: .touchUpInside)
        self.addSubview(storeBtn)
        
        shoppingCarBtn = UIButton()
        shoppingCarBtn.setup_image(image: UIImage.init(named: "icon_shopping_car"))
        shoppingCarBtn.addTarget(self, action: #selector(self.handleShoppingCar), for: .touchUpInside)
        self.addSubview(shoppingCarBtn)
        self.addSubview(self.redView)
        
        likeBtn = UIButton()
        likeBtn.setup_image(image: UIImage.init(named: "icon_used_car_collect"))
        likeBtn.addTarget(self, action: #selector(self.handleLike), for: .touchUpInside)
        self.addSubview(likeBtn)
        
        //底线
        self.addSubview(bottomLineView)
        
        self.addSnpLayout()
        self.clipsToBounds = true
    }
    
    private func addSnpLayout(){
        let tempW = XTManageCenter.getLabWidth(labelStr: serviceBtn.title, font: UIFont.init(name: Theme.PingFangSCRegular, size: 18)!, height: 20)
        serviceBtn.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.top.equalTo(IphoneTopArea)
            make.height.equalTo(44)
            make.width.equalTo(tempW + 10)
        }
        
        let tempW1 = XTManageCenter.getLabWidth(labelStr: storeBtn.title, font: UIFont.init(name: Theme.PingFangSCRegular, size: 18)!, height: 20)
        storeBtn.snp.makeConstraints { make in
            make.left.equalTo(serviceBtn.snp.right).offset(24)
            make.top.equalTo(IphoneTopArea)
            make.height.equalTo(44)
            make.width.equalTo(tempW1 + 10)
        }
        likeBtn.snp.makeConstraints { make in
            make.right.equalTo(-14)
            make.top.equalTo(IphoneTopArea)
            make.width.equalTo(44)
            make.height.equalTo(44)
        }
        
        shoppingCarBtn.snp.makeConstraints { make in
            make.right.equalTo(likeBtn.snp.left)
            make.top.equalTo(IphoneTopArea)
            make.width.equalTo(44)
            make.height.equalTo(44)
        }
        
        redView.snp.makeConstraints { make in
            make.centerX.equalTo(shoppingCarBtn.snp.centerX).offset(10)
            make.centerY.equalTo(shoppingCarBtn.snp.centerY).offset(-10)
            make.height.equalTo(12)
            make.width.equalTo(16)
        }
        
        bottomLineView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.height.equalTo(0.5)
            make.bottom.equalTo(-0.5)
        }
        
        self.layoutIfNeeded()
    }
    
    ///服务
    @objc func serviceBtnAction(){
        self.serviceBtn.isSelected = true
        self.storeBtn.isSelected = false
        self.callBack?(MaintenceItem.service)
    }
    
    ///商城
    @objc func storeBtnAction(){
        self.serviceBtn.isSelected = false
        self.storeBtn.isSelected = true
        self.callBack?(MaintenceItem.store)
    }
    
    ///购物车
    @objc func handleShoppingCar(){
        self.callBack?(MaintenceItem.shoppingCar)

    }
    
    ///收藏
    @objc func handleLike(){
        self.callBack?(MaintenceItem.favory)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


